package controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import dao.LoaisanphamDAO;
import dao.NhasanxuatDAO;
import dao.SanphamDAO;
import dao.ThamsochungDAO;

import pojo.Loaisanpham;
import pojo.Nhasanxuat;
import pojo.Sanpham;
import util.ConvertHelper;
import util.CountVisitor;

@ManagedBean(name = "trangTimKiemNCBean")
@SessionScoped
public class TrangTimKiemNCBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<Sanpham> lstSanPham = new ArrayList<Sanpham>();
	private List<Loaisanpham> lstLoaiSanPham = new ArrayList<Loaisanpham>();
	private List<Nhasanxuat> lstNhaSX = new ArrayList<Nhasanxuat>();

	private String tensanpham;
	private String maloaisanpham;
	private String manhasanxuat;
	private String tugiatien;
	private String dengiatien;

	public TrangTimKiemNCBean() {
		System.out.println("vao trang khoi tao TrangTimKiemNCBean");
		Init();
	}

	public void Init() {
		LoaisanphamDAO lspDAO = new LoaisanphamDAO();
		NhasanxuatDAO nsxDAO = new NhasanxuatDAO();

		lstLoaiSanPham = lspDAO.getList();
		lstNhaSX = nsxDAO.getList();

		System.out.println("sl loai sp: " + lstLoaiSanPham.size());
		System.out.println("sl nsx: " + lstNhaSX.size());
	}

	public String TimKiem() {
		Map<String, String> params = FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap();
		String thututrang = params.get("thututrang");

		int index = 1;
		if (thututrang != null)
			index = Integer.parseInt(thututrang);

		int nSPTrenTrang = LaySoSPTrenTrang();

		int FromIndex = nSPTrenTrang * (index - 1);

		SanphamDAO spDAO = new SanphamDAO();
		// setLstSanPham(spDAO.SearchSanPham(FromIndex, nSPTrenTrang,
		// ConvertHelper.ConvertCP1252ToUTF8(tensanpham), maloaisanpham,
		// manhasanxuat, tugiatien, dengiatien));
		setLstSanPham(spDAO.SearchSanPham(FromIndex, nSPTrenTrang, tensanpham,
				maloaisanpham, manhasanxuat, tugiatien, dengiatien));
		System.out.println("da vao tim kiem");

		return "TrangKetQuaTimKiemNC?faces-redirect=true";
	}

	public String test() {
		System.out.println("Da vao ham test");
		return "TrangKetQuaTimKiemNC?faces-redirect=true";
	}

	public int LaySoSPTrenTrang() {
		ThamsochungDAO thamsochung = new ThamsochungDAO();
		return Integer.parseInt(thamsochung.LayGiaTriCuaThamSo(
				ThamsochungDAO.TenThamSo.SO_SAN_PHAM_TREN_TRANG).toString());
	}

	public int XacDinhSoTrang() {
		SanphamDAO spdao = new SanphamDAO();

		int nSPTrenTrang = LaySoSPTrenTrang();
		// int nSanPham =
		// spdao.CountSearchSanPham(ConvertHelper.ConvertCP1252ToUTF8(tensanpham),
		// maloaisanpham, manhasanxuat, tugiatien, dengiatien);
		int nSanPham = spdao.CountSearchSanPham(tensanpham, maloaisanpham,
				manhasanxuat, tugiatien, dengiatien);
		System.out.println("XacDinhSoTrang so luong sp: " + nSanPham);
		int nTrang = nSanPham / nSPTrenTrang;
		nTrang = nSanPham % nSPTrenTrang == 0 ? nTrang : nTrang + 1;

		return nTrang;
	}

	public List<Loaisanpham> getLstLoaiSanPham() {
		return lstLoaiSanPham;
	}

	public void setLstLoaiSanPham(List<Loaisanpham> lstLoaiSanPham) {
		this.lstLoaiSanPham = lstLoaiSanPham;
	}

	public List<Nhasanxuat> getLstNhaSX() {
		return lstNhaSX;
	}

	public void setLstNhaSX(List<Nhasanxuat> lstNhaSX) {
		this.lstNhaSX = lstNhaSX;
	}

	public String getTensanpham() {
		return tensanpham;
	}

	public void setTensanpham(String tensanpham) {
		this.tensanpham = tensanpham;
	}

	public String getMaloaisanpham() {
		return maloaisanpham;
	}

	public void setMaloaisanpham(String maloaisanpham) {
		this.maloaisanpham = maloaisanpham;
	}

	public String getManhasanxuat() {
		return manhasanxuat;
	}

	public void setManhasanxuat(String manhasanxuat) {
		this.manhasanxuat = manhasanxuat;
	}

	public String getTugiatien() {
		return tugiatien;
	}

	public void setTugiatien(String tugiatien) {
		this.tugiatien = tugiatien;
	}

	public String getDengiatien() {
		return dengiatien;
	}

	public void setDengiatien(String dengiatien) {
		this.dengiatien = dengiatien;
	}

	public List<Sanpham> getLstSanPham() {
		return lstSanPham;
	}

	public void setLstSanPham(List<Sanpham> lstSanPham) {
		this.lstSanPham = lstSanPham;
	}

}
